package com.example.petshopping.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petstore.PetStoreDetailActivity;
import com.example.petshopping.R;
import com.example.petshopping.adapter.StoreAdapter;
import com.example.petshopping.model.Product;
import com.example.petshopping.model.Store;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 店铺排行页面
 */
public class StoreRankingActivity extends AppCompatActivity implements StoreAdapter.OnStoreClickListener {
    
    private RecyclerView rvStores;
    private StoreAdapter storeAdapter;
    
    // 主分类布局
    private LinearLayout layoutCategoryPet, layoutCategoryAquatic, layoutCategory99;
    
    // 主分类文本
    private TextView tvCategoryPet, tvCategoryAquatic, tvCategory99;
    
    // 主分类下划线
    private View viewUnderlinePet, viewUnderlineAquatic, viewUnderline99;
    
    // 筛选分类
    private TextView tvFilterMonthly, tvFilterTop10, tvFilterPetStore, tvFilterPetShop;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_ranking);
        
        // 初始化视图
        initViews();
        
        // 初始化数据
        initData();
        
        // 设置点击事件
        setupClickEvents();
    }
    
    private void initViews() {
        // 主分类布局
        layoutCategoryPet = findViewById(R.id.layoutCategoryPet);
        layoutCategoryAquatic = findViewById(R.id.layoutCategoryAquatic);
        layoutCategory99 = findViewById(R.id.layoutCategory99);
        
        // 主分类文本
        tvCategoryPet = findViewById(R.id.tvCategoryPet);
        tvCategoryAquatic = findViewById(R.id.tvCategoryAquatic);
        tvCategory99 = findViewById(R.id.tvCategory99);
        
        // 主分类下划线
        viewUnderlinePet = findViewById(R.id.viewUnderlinePet);
        viewUnderlineAquatic = findViewById(R.id.viewUnderlineAquatic);
        viewUnderline99 = findViewById(R.id.viewUnderline99);
        
        // 筛选分类
        tvFilterMonthly = findViewById(R.id.tvFilterMonthly);
        tvFilterTop10 = findViewById(R.id.tvFilterTop10);
        tvFilterPetStore = findViewById(R.id.tvFilterPetStore);
        tvFilterPetShop = findViewById(R.id.tvFilterPetShop);
        
        // 店铺列表
        rvStores = findViewById(R.id.rvStores);
        rvStores.setLayoutManager(new LinearLayoutManager(this));
        storeAdapter = new StoreAdapter(this, null, this);
        rvStores.setAdapter(storeAdapter);
    }
    
    private void initData() {
        // 模拟数据
        List<Store> storeList = generateMockStores();
        storeAdapter.updateData(storeList);
    }
    
    private List<Store> generateMockStores() {
        List<Store> stores = new ArrayList<>();
        
        // 店铺1
        List<Product> products1 = Arrays.asList(
                new Product("1", "水族箱", "yugang", "¥432"),
                new Product("2", "仓鼠", "pet_2", "¥432"),
                new Product("3", "水族植物", "pet_4", "¥432"),
                new Product("4", "宠物用品", "pet_5", "¥432")
        );
        stores.add(new Store("1", "招财猫旺财狗", "yingwu", "★★★★★", "44万粉丝", products1));
        
        // 店铺2
        List<Product> products2 = Arrays.asList(
                new Product("5", "水族箱", "pet_3", "¥432"),
                new Product("6", "仓鼠", "pet_2", "¥432"),
                new Product("7", "水族植物", "pet_4", "¥432"),
                new Product("8", "宠物用品", "pet_5", "¥432")
        );
        stores.add(new Store("2", "招财猫旺财狗", "yingwu", "★★★★★", "44万粉丝", products2));
        
        // 店铺3
        List<Product> products3 = Arrays.asList(
                new Product("9", "水族箱", "pet_3", "¥432"),
                new Product("10", "仓鼠", "pet_2", "¥432"),
                new Product("11", "水族植物", "pet_4", "¥432"),
                new Product("12", "宠物用品", "pet_5", "¥432")
        );
        stores.add(new Store("3", "招财猫旺财狗", "yingwu", "★★★★★", "44万粉丝", products3));
        
        // 店铺4
        List<Product> products4 = Arrays.asList(
                new Product("13", "水族箱", "pet_3", "¥432"),
                new Product("14", "仓鼠", "pet_2", "¥432"),
                new Product("15", "水族植物", "pet_4", "¥432"),
                new Product("16", "宠物用品", "pet_5", "¥432")
        );
        stores.add(new Store("4", "招财猫旺财狗", "yingwu", "★★★★★", "44万粉丝", products4));
        
        return stores;
    }
    
    private void setupClickEvents() {
        // 主分类点击事件
        layoutCategoryPet.setOnClickListener(v -> {
            updateMainCategorySelection(layoutCategoryPet);
            Toast.makeText(this, "已选择：宠物", Toast.LENGTH_SHORT).show();
        });
        
        layoutCategoryAquatic.setOnClickListener(v -> {
            updateMainCategorySelection(layoutCategoryAquatic);
            Toast.makeText(this, "已选择：水族", Toast.LENGTH_SHORT).show();
        });
        
        layoutCategory99.setOnClickListener(v -> {
            updateMainCategorySelection(layoutCategory99);
            Toast.makeText(this, "已选择：9.9包邮", Toast.LENGTH_SHORT).show();
        });
        
        // 筛选分类点击事件
        tvFilterMonthly.setOnClickListener(v -> {
            updateFilterSelection(tvFilterMonthly);
            Toast.makeText(this, "已筛选：月度", Toast.LENGTH_SHORT).show();
        });
        
        tvFilterTop10.setOnClickListener(v -> {
            updateFilterSelection(tvFilterTop10);
            Toast.makeText(this, "已筛选：十佳", Toast.LENGTH_SHORT).show();
        });
        
        tvFilterPetStore.setOnClickListener(v -> {
            updateFilterSelection(tvFilterPetStore);
            Toast.makeText(this, "已筛选：宠物店铺", Toast.LENGTH_SHORT).show();
        });
        
        tvFilterPetShop.setOnClickListener(v -> {
            updateFilterSelection(tvFilterPetShop);
            Toast.makeText(this, "已筛选：卖卖宠物一站购", Toast.LENGTH_SHORT).show();
        });
    }
    
    // 更新主分类选择状态
    private void updateMainCategorySelection(LinearLayout selectedLayout) {
        // 重置所有下划线为不可见
        viewUnderlinePet.setVisibility(View.INVISIBLE);
        viewUnderlineAquatic.setVisibility(View.INVISIBLE);
        viewUnderline99.setVisibility(View.INVISIBLE);
        
        // 重置所有文本样式为非粗体
        tvCategoryPet.setTypeface(null, android.graphics.Typeface.NORMAL);
        tvCategoryAquatic.setTypeface(null, android.graphics.Typeface.NORMAL);
        tvCategory99.setTypeface(null, android.graphics.Typeface.NORMAL);
        
        // 根据选中的布局设置对应的下划线和文本样式
        if (selectedLayout == layoutCategoryPet) {
            viewUnderlinePet.setVisibility(View.VISIBLE);
            tvCategoryPet.setTypeface(null, android.graphics.Typeface.BOLD);
        } else if (selectedLayout == layoutCategoryAquatic) {
            viewUnderlineAquatic.setVisibility(View.VISIBLE);
            tvCategoryAquatic.setTypeface(null, android.graphics.Typeface.BOLD);
        } else if (selectedLayout == layoutCategory99) {
            viewUnderline99.setVisibility(View.VISIBLE);
            tvCategory99.setTypeface(null, android.graphics.Typeface.BOLD);
        }
    }
    
    // 更新筛选选择状态
    private void updateFilterSelection(TextView selectedFilter) {
        // 重置所有筛选样式
        tvFilterMonthly.setBackgroundResource(R.drawable.bg_filter_normal);
//        tvFilterMonthly.setTextColor(getResources().getColor(android.R.color.black));
        
        tvFilterTop10.setBackgroundResource(R.drawable.bg_filter_normal);
//        tvFilterTop10.setTextColor(getResources().getColor(android.R.color.black));
        
        tvFilterPetStore.setBackgroundResource(R.drawable.bg_filter_normal);
//        tvFilterPetStore.setTextColor(getResources().getColor(android.R.color.black));
        
        tvFilterPetShop.setBackgroundResource(R.drawable.bg_filter_normal);
//        tvFilterPetShop.setTextColor(getResources().getColor(android.R.color.black));
        
        // 设置选中筛选样式
        selectedFilter.setBackgroundResource(R.drawable.bg_filter_selected);
        selectedFilter.setTextColor(getResources().getColor(R.color.purple_500));
    }
    
    // 店铺点击事件回调实现
    @Override
    public void onStoreClick(Store store) {
        Toast.makeText(this, "点击了店铺: " + store.getName(), Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onEnterStoreClick(Store store) {
        // 跳转到PetStoreDetailActivity
        Intent intent = new Intent(this, PetStoreDetailActivity.class);
        // 可以传递店铺信息
        intent.putExtra("storeName", store.getName());
        intent.putExtra("storeRating", store.getRating());
        intent.putExtra("storeFollowers", store.getFollowers());
        startActivity(intent);
    }
} 